<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="Company" prop="companyId">
        <el-input
          v-model="queryParams.companyId"
          placeholder="请输入Company"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Name" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入Name"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Status" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入Status"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Payment Reference" prop="paymentReference">
        <el-input
          v-model="queryParams.paymentReference"
          placeholder="请输入Payment Reference"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Journal Entry Name" prop="moveName">
        <el-input
          v-model="queryParams.moveName"
          placeholder="请输入Journal Entry Name"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Transfer To" prop="destinationJournalId">
        <el-input
          v-model="queryParams.destinationJournalId"
          placeholder="请输入Transfer To"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Payment Difference" prop="paymentDifferenceHandling">
        <el-input
          v-model="queryParams.paymentDifferenceHandling"
          placeholder="请输入Payment Difference"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Difference Account" prop="writeoffAccountId">
        <el-input
          v-model="queryParams.writeoffAccountId"
          placeholder="请输入Difference Account"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Journal Item Label" prop="writeoffLabel">
        <el-input
          v-model="queryParams.writeoffLabel"
          placeholder="请输入Journal Item Label"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Last Message Date">
        <el-date-picker
          v-model="daterangeMessageLastPost"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="Payment Method Type" prop="paymentMethodId">
        <el-input
          v-model="queryParams.paymentMethodId"
          placeholder="请输入Payment Method Type"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Partner" prop="partnerId">
        <el-input
          v-model="queryParams.partnerId"
          placeholder="请输入Partner"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Payment Amount" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入Payment Amount"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Currency" prop="currencyId">
        <el-input
          v-model="queryParams.currencyId"
          placeholder="请输入Currency"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Payment Date" prop="paymentDate">
        <el-date-picker clearable
          v-model="queryParams.paymentDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择Payment Date">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="Payment Journal" prop="journalId">
        <el-input
          v-model="queryParams.journalId"
          placeholder="请输入Payment Journal"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Created by" prop="createUid">
        <el-select v-model="queryParams.createUid" placeholder="请选择Created by" clearable>
          <el-option
            v-for="dict in dict.type.boshien_emp"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="Created on">
        <el-date-picker
          v-model="daterangeCreateDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="Last Updated by" prop="writeUid">
        <el-select v-model="queryParams.writeUid" placeholder="请选择Last Updated by" clearable>
          <el-option
            v-for="dict in dict.type.boshien_emp"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="Last Updated on">
        <el-date-picker
          v-model="daterangeWriteDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="Payment Transaction" prop="paymentTransactionId">
        <el-input
          v-model="queryParams.paymentTransactionId"
          placeholder="请输入Payment Transaction"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Saved payment token" prop="paymentTokenId">
        <el-input
          v-model="queryParams.paymentTokenId"
          placeholder="请输入Saved payment token"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:payment:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:payment:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:payment:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:payment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="paymentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="${comment}" align="center" prop="id" />
      <el-table-column label="Company" align="center" prop="companyId" />
      <el-table-column label="Name" align="center" prop="name" />
      <el-table-column label="Status" align="center" prop="state" />
      <el-table-column label="Payment Type" align="center" prop="paymentType" />
      <el-table-column label="Payment Reference" align="center" prop="paymentReference" />
      <el-table-column label="Journal Entry Name" align="center" prop="moveName" />
      <el-table-column label="Transfer To" align="center" prop="destinationJournalId" />
      <el-table-column label="Payment Difference" align="center" prop="paymentDifferenceHandling" />
      <el-table-column label="Difference Account" align="center" prop="writeoffAccountId" />
      <el-table-column label="Journal Item Label" align="center" prop="writeoffLabel" />
      <el-table-column label="Last Message Date" align="center" prop="messageLastPost" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.messageLastPost, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="Payment Method Type" align="center" prop="paymentMethodId" />
      <el-table-column label="Partner Type" align="center" prop="partnerType" />
      <el-table-column label="Partner" align="center" prop="partnerId" />
      <el-table-column label="Payment Amount" align="center" prop="amount" />
      <el-table-column label="Currency" align="center" prop="currencyId" />
      <el-table-column label="Payment Date" align="center" prop="paymentDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paymentDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="Memo" align="center" prop="communication" />
      <el-table-column label="Payment Journal" align="center" prop="journalId" />
      <el-table-column label="Created by" align="center" prop="createUid">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.boshien_emp" :value="scope.row.createUid"/>
        </template>
      </el-table-column>
      <el-table-column label="Created on" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="Last Updated by" align="center" prop="writeUid">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.boshien_emp" :value="scope.row.writeUid"/>
        </template>
      </el-table-column>
      <el-table-column label="Last Updated on" align="center" prop="writeDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.writeDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="Payment Transaction" align="center" prop="paymentTransactionId" />
      <el-table-column label="Saved payment token" align="center" prop="paymentTokenId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:payment:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:payment:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改付款对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="Company" prop="companyId">
          <el-input v-model="form.companyId" placeholder="请输入Company" />
        </el-form-item>
        <el-form-item label="Name" prop="name">
          <el-input v-model="form.name" placeholder="请输入Name" />
        </el-form-item>
        <el-form-item label="Status" prop="state">
          <el-input v-model="form.state" placeholder="请输入Status" />
        </el-form-item>
        <el-form-item label="Payment Reference" prop="paymentReference">
          <el-input v-model="form.paymentReference" placeholder="请输入Payment Reference" />
        </el-form-item>
        <el-form-item label="Journal Entry Name" prop="moveName">
          <el-input v-model="form.moveName" placeholder="请输入Journal Entry Name" />
        </el-form-item>
        <el-form-item label="Transfer To" prop="destinationJournalId">
          <el-input v-model="form.destinationJournalId" placeholder="请输入Transfer To" />
        </el-form-item>
        <el-form-item label="Payment Difference" prop="paymentDifferenceHandling">
          <el-input v-model="form.paymentDifferenceHandling" placeholder="请输入Payment Difference" />
        </el-form-item>
        <el-form-item label="Difference Account" prop="writeoffAccountId">
          <el-input v-model="form.writeoffAccountId" placeholder="请输入Difference Account" />
        </el-form-item>
        <el-form-item label="Journal Item Label" prop="writeoffLabel">
          <el-input v-model="form.writeoffLabel" placeholder="请输入Journal Item Label" />
        </el-form-item>
        <el-form-item label="Last Message Date" prop="messageLastPost">
          <el-date-picker clearable
            v-model="form.messageLastPost"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择Last Message Date">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="Payment Method Type" prop="paymentMethodId">
          <el-input v-model="form.paymentMethodId" placeholder="请输入Payment Method Type" />
        </el-form-item>
        <el-form-item label="Partner" prop="partnerId">
          <el-input v-model="form.partnerId" placeholder="请输入Partner" />
        </el-form-item>
        <el-form-item label="Payment Amount" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入Payment Amount" />
        </el-form-item>
        <el-form-item label="Currency" prop="currencyId">
          <el-input v-model="form.currencyId" placeholder="请输入Currency" />
        </el-form-item>
        <el-form-item label="Payment Date" prop="paymentDate">
          <el-date-picker clearable
            v-model="form.paymentDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择Payment Date">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="Memo" prop="communication">
          <el-input v-model="form.communication" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="Payment Journal" prop="journalId">
          <el-input v-model="form.journalId" placeholder="请输入Payment Journal" />
        </el-form-item>
        <el-form-item label="Created by" prop="createUid">
          <el-select v-model="form.createUid" placeholder="请选择Created by">
            <el-option
              v-for="dict in dict.type.boshien_emp"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="Created on" prop="createDate">
          <el-date-picker clearable
            v-model="form.createDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择Created on">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="Last Updated by" prop="writeUid">
          <el-select v-model="form.writeUid" placeholder="请选择Last Updated by">
            <el-option
              v-for="dict in dict.type.boshien_emp"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="Last Updated on" prop="writeDate">
          <el-date-picker clearable
            v-model="form.writeDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择Last Updated on">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="Payment Transaction" prop="paymentTransactionId">
          <el-input v-model="form.paymentTransactionId" placeholder="请输入Payment Transaction" />
        </el-form-item>
        <el-form-item label="Saved payment token" prop="paymentTokenId">
          <el-input v-model="form.paymentTokenId" placeholder="请输入Saved payment token" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPayment, getPayment, delPayment, addPayment, updatePayment } from "@/api/system/payment";

export default {
  name: "Payment",
  dicts: ['boshien_emp'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 付款表格数据
      paymentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // Saved payment token时间范围
      daterangeMessageLastPost: [],
      // Saved payment token时间范围
      daterangeCreateDate: [],
      // Saved payment token时间范围
      daterangeWriteDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        name: null,
        state: null,
        paymentType: null,
        paymentReference: null,
        moveName: null,
        destinationJournalId: null,
        paymentDifferenceHandling: null,
        writeoffAccountId: null,
        writeoffLabel: null,
        messageLastPost: null,
        paymentMethodId: null,
        partnerType: null,
        partnerId: null,
        amount: null,
        currencyId: null,
        paymentDate: null,
        communication: null,
        journalId: null,
        createUid: null,
        createDate: null,
        writeUid: null,
        writeDate: null,
        paymentTransactionId: null,
        paymentTokenId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        paymentType: [
          { required: true, message: "Payment Type不能为空", trigger: "change" }
        ],
        paymentMethodId: [
          { required: true, message: "Payment Method Type不能为空", trigger: "blur" }
        ],
        amount: [
          { required: true, message: "Payment Amount不能为空", trigger: "blur" }
        ],
        currencyId: [
          { required: true, message: "Currency不能为空", trigger: "blur" }
        ],
        paymentDate: [
          { required: true, message: "Payment Date不能为空", trigger: "blur" }
        ],
        journalId: [
          { required: true, message: "Payment Journal不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询付款列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeMessageLastPost && '' != this.daterangeMessageLastPost) {
        this.queryParams.params["beginMessageLastPost"] = this.daterangeMessageLastPost[0];
        this.queryParams.params["endMessageLastPost"] = this.daterangeMessageLastPost[1];
      }
      if (null != this.daterangeCreateDate && '' != this.daterangeCreateDate) {
        this.queryParams.params["beginCreateDate"] = this.daterangeCreateDate[0];
        this.queryParams.params["endCreateDate"] = this.daterangeCreateDate[1];
      }
      if (null != this.daterangeWriteDate && '' != this.daterangeWriteDate) {
        this.queryParams.params["beginWriteDate"] = this.daterangeWriteDate[0];
        this.queryParams.params["endWriteDate"] = this.daterangeWriteDate[1];
      }
      listPayment(this.queryParams).then(response => {
        this.paymentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        companyId: null,
        name: null,
        state: null,
        paymentType: null,
        paymentReference: null,
        moveName: null,
        destinationJournalId: null,
        paymentDifferenceHandling: null,
        writeoffAccountId: null,
        writeoffLabel: null,
        messageLastPost: null,
        paymentMethodId: null,
        partnerType: null,
        partnerId: null,
        amount: null,
        currencyId: null,
        paymentDate: null,
        communication: null,
        journalId: null,
        createUid: null,
        createDate: null,
        writeUid: null,
        writeDate: null,
        paymentTransactionId: null,
        paymentTokenId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeMessageLastPost = [];
      this.daterangeCreateDate = [];
      this.daterangeWriteDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加付款";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPayment(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改付款";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePayment(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPayment(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除付款编号为"' + ids + '"的数据项？').then(function() {
        return delPayment(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/payment/export', {
        ...this.queryParams
      }, `payment_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
